//
//  LGDiseaseOverviewInspectCell.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/16.
//

#import "LGDiseaseOverviewInspectCell.h"
#import "LGDiseaseOverviewInspectHeader.h"
#import "LGAnomalyInspectCell.h"
@interface LGDiseaseOverviewInspectCell()<UITableViewDelegate,UITableViewDataSource>
/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , assign)CGFloat tableHeight;
@end

@implementation LGDiseaseOverviewInspectCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.clipsToBounds = YES;
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:self.baseView];
        [self.contentView addSubview:self.tableView];
        [self.baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(viewPix(16));
            make.right.equalTo(self.contentView).offset(-viewPix(16));
            make.top.bottom.equalTo(self.contentView);
        }];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.left.right.equalTo(self.contentView);
            make.height.equalTo(@(viewPix(50)));
        }];
    }
    return  self;
}

-(void)setTableHeight:(CGFloat)tableHeight{
    _tableHeight = tableHeight;
}

-(void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [UIView performWithoutAnimation:^{
          [self.tableView reloadData];
      }];
      [self performSelector:@selector(refreshHeight) withObject:nil afterDelay:0.02];
}

-(void)refreshHeight{
    if (self.tableHeight != self.tableView.contentSize.height) {
        [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(self.tableView.contentSize.height));
        }];
        [self.tableView layoutIfNeeded];
        self.tableHeight = self.tableView.contentSize.height;
        [UIView performWithoutAnimation:^{
            [self.fatherTableView reloadData];
        }];
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSArray *tempArray = self.dataArray[section][@"lists"];
    return tempArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGAnomalyInspectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSArray *tempArray = self.dataArray[indexPath.section][@"lists"];
    cell.type = @"middle";
    cell.dataDic = tempArray[indexPath.row];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //异常指标趋势
    if(self.checkInspectChart){
        self.checkInspectChart(self.dataArray[indexPath.section][@"lists"][indexPath.row]);
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(60);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGDiseaseOverviewInspectHeader *headView = [[LGDiseaseOverviewInspectHeader alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(60))];
    headView.dateString = LGNSString(self.dataArray[section][@"report_time"]);
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    LGCornerFooterView *footView = [[LGCornerFooterView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 5)];
    footView.backgroundColor = [UIColor clearColor];
    footView.bottomMargin = -viewPix(16);
    return footView;
}

-(UITableView *)superTableView{
    UIView *tableView = self.superview;
    while (![tableView isKindOfClass:[UITableView class]] && tableView) {
        tableView = tableView.superview;
    }
    return (UITableView *)tableView;
}

-(UIView *)baseView{
    if(!_baseView){
        _baseView = [UIView shadowViewWithColor:@"#B2BDC7" offset:CGSizeMake(0, 2) opacity:0.15 radius:0];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(50)) style:UITableViewStylePlain];
        [_tableView registerClass:[LGAnomalyInspectCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.scrollEnabled = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
